package org.budo.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.budo.support.lang.util.NumberUtil;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public abstract class AbstractXmlObject implements XmlObject {
    @Override
    public String getAttributeValue(String attributeName) {
        Map<String, String> attributeMap = this.getAttributeMap();
        if (null == attributeMap) {
            return null;
        }

        return attributeMap.get(attributeName);
    }

    public XmlObject[] getChildNodeArrayByName(String nodeName) {
        XmlObject[] childNodeArray = this.getChildNodeArray();
        if (null == childNodeArray || null == nodeName) {
            return null;
        }

        List<XmlObject> xmlObjects = new ArrayList<XmlObject>();
        for (int i = 0; i < childNodeArray.length; i++) {
            XmlObject child = childNodeArray[i];
            if (nodeName.equals(child.getNodeName())) {
                xmlObjects.add(child);
            }
        }

        return xmlObjects.toArray(new XmlObject[xmlObjects.size()]);
    }

    public XmlObject getChildNodeByName(String nodeName) {
        XmlObject[] childNodeArray = this.getChildNodeArray();

        if (null == childNodeArray || null == nodeName) {
            return null;
        }

        for (int i = 0; i < childNodeArray.length; i++) {
            XmlObject child = childNodeArray[i];
            if (nodeName.equals(child.getNodeName())) {
                return child;
            }
        }

        return null;
    }

    @Override
    public String getChildNodeTextContentByName(String nodeName) {
        XmlObject childNode = this.getChildNodeByName(nodeName);
        return null == childNode ? null : childNode.getTextContent();
    }

    @Override
    public Integer getAttributeValueInteger(String attributeName) {
        String attributeValue = this.getAttributeValue(attributeName);
        return NumberUtil.toInteger(attributeValue);
    }

    @Override
    public String getAttributeValue(String attributeName, String defaultValue) {
        String attributeValue = this.getAttributeValue(attributeName);
        return StringUtil.isEmpty(attributeValue) ? defaultValue : attributeValue;
    }

    @Override
    public Integer getAttributeValueInteger(String attributeName, Integer defaultValue) {
        String attributeValue = this.getAttributeValue(attributeName);
        return NumberUtil.toInteger(attributeValue, defaultValue);
    }

    @Override
    public String toString() {
        String nodeName = this.getNodeName();
        String xml = "<" + nodeName;

        Map<String, String> attributeMap = this.getAttributeMap();
        if (null != attributeMap && !attributeMap.isEmpty()) {
            Set<Entry<String, String>> attributeSet = attributeMap.entrySet();
            for (Entry<String, String> attribute : attributeSet) {
                xml += " " + attribute.getKey() + "=\"" + attribute.getValue() + "\"";
            }
        }

        xml += ">";
        XmlObject[] childNodeArray = this.getChildNodeArray();
        if (null != childNodeArray && childNodeArray.length > 0) {
            for (XmlObject childNode : childNodeArray) {
                xml += childNode.toString();
            }
        } else {
            xml += this.getTextContent();
        }

        xml += "</" + nodeName + ">";

        String xml_2 = xml.replace("<#document>", "") //
                .replace("</#document>", "") //
                .replace("<#text>", "") //
                .replace("</#text>", "") //
                .replace("<#cdata-section>", "<![CDATA[") //
                .replace("</#cdata-section>", "]]>");

        return xml_2;
    }
}
